var app = getApp();
var util = require('../../utils/util.js');

Page({
  data: {
    date: '请选择出生日期',
    age: ''
  },
  onLoad: function() {},
  // 年月日选择
  bindDateChange(e) {
    let age = util.countAge(e.detail.value).toString(); // 计算年龄
    this.setData({
      date: e.detail.value,
      age: age
    })
  },
  // 提交表单-基本信息
  formSubmitBaseInfo: function(e) {
    var telephone = wx.getStorageSync('telephone');
    var that = this;
    var formObj = e.detail.value;
    formObj.telephone = telephone;
    formObj.age = this.data.age;

    if (formObj.name != '' && formObj.age != '' && formObj.sex != '') {
      wx.request({
        url: app.globalData.apiBaseUrl + '/wechat/updateMedicalUserInfo',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'FilterToken_MT': wx.getStorageSync("token")
        },
        method: 'POST',
        data: formObj,
        dataType: 'json',
        success: function(res) {
          res = res.data;
          if (res.code == 200) {
            var age = Number(formObj.age);
            if (age > 15) {
              wx.setStorageSync("isChild", false)
            } else {
              wx.setStorageSync("isChild", true)
            }
            wx.redirectTo({
              url: "../questionnaire/questionnaire"
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 3000
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请完善填写信息',
        icon: 'none',
        duration: 3000
      })
    }
  }
})